import ujson
import datetime
import time

from 获取监测点信息 import *
from 操作消息队列 import *

def msg(nodes,**kwargs):
    # 获取当前时间
    now = datetime.datetime.now()

    acode = kwargs['acode']
    values = kwargs['values'].split(',')

    json = {}
    # 根据nodes的元素个数进行循环
    for i in range(len(nodes)):
        # 获取第i个元素
        node = nodes[i]
        nodeid = node[1]
        if nodeid == '00000000000000000001':
            json[nodeid] = '1'
        else:
            json[nodeid] = values[i]

    json['DATETIME_ACQUISITION'] = str(now)
    json['DEVICE_CODE'] = acode

    if kwargs['workLocation'] != '':
        json['WORK_LOCATION'] = kwargs['workLocation']

    if kwargs['testFormNumber'] != '':
        json['TESTORMNUMBER'] = kwargs['testFormNumber']

    json_str = ujson.dumps(json)
    return json_str

def send_msg(**kwargs):
    queueName = kwargs['queueName']
    # 上面的发送消息代码，循环调用100次
    connection, channel = conn_rabbitmq()
    nodes = select_scadaequipnodeinfo(kwargs['equipmentNumber'])
    while True:
        for i in range(int(kwargs['count'])):
            json_str = msg(nodes,**kwargs)
            channel.basic_publish(exchange='', routing_key=queueName, body=json_str)
            # 输出显示：发送完成：json_str
            print(str(i)+"发送完成:"+json_str)

        # print(str(i)+"发送完成"+int(kwargs['count']))
    # # 关闭连接
    # connection.close()


if __name__ == '__main__':

    # 无限循环
        # 延时10秒
        # time.sleep(10)
        # params = {
        #     'equipmentNumber':'虚拟设备4',
        #     'acode':'虚拟设备4',
        #     'queueName':'mca_queue',
        #     'workLocation':'4-2-10',
        #     'testFormNumber':'ST2024050014',
        #     'count':'100',
        #     'values':'9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9'
        # }
        # send_msg(**params)

    params = {
        'equipmentNumber': '虚拟设备4',
        'acode': '虚拟设备4',
        'queueName': 'test_queue',
        'workLocation': '4-2-10',
        'testFormNumber': 'ST2024050014',
        'count': '100',
        'values': '-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9'
    }
    send_msg(**params)

        # params = {
        #     'equipmentNumber': '虚拟设备4',
        #     'acode': '虚拟设备4',
        #     'queueName': 'test_queue',
        #     'workLocation': '23-3-18',
        #     'testFormNumber': 'ST2024080028',
        #     'count': '100',
        #     'values': '0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4'
        # }
        # send_msg(**params)



    # params = {
    #     'equipmentNumber':'虚拟设备4',
    #     'acode':'虚拟设备4',
    #     'queueName':'test_queue',
    #     'workLocation':'4-1-2',
    #     'testFormNumber':'ST2025030069',
    #     'count':'100',
    #     'values':'122,111,112,113,124,56,43,34,89,45,67,89,12,34,56,78,65,67,89,67,56,45,32,12,345,567,98'
    # }
    # send_msg(**params)